/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
  */
package jFamilyTree.commons;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;

/**
 * Import dialog
 * 
 * @author Bernard Bou
 */
public class OpenDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	// V A L U E S

	/**
	 * Properties (input/output)
	 */
	protected Properties theProperties;

	/**
	 * Ok result
	 */
	public boolean ok;

	// C O M P O N E N T S

	/**
	 * Source text
	 */
	private JTextField theSourceTextField;

	/**
	 * Provider text
	 */
	private JComboBox theProviderComboBox;

	/**
	 * Data panel
	 */
	protected JPanel theDataPanel;

	/**
	 * Constructor
	 * 
	 * @param theseProperties
	 *        settings
	 */
	public OpenDialog(Properties theseProperties)
	{
		super();
		theProperties = theseProperties;
		if (theProperties == null)
			theProperties = new Properties();

		Set<String> thesePresetProviders = Searcher.findClasses(".*\\.Provider"); //$NON-NLS-1$
		initialize(thesePresetProviders);
	}

	/**
	 * Initialize
	 */
	@SuppressWarnings("serial")
	protected void initialize(Collection<String> theseProviders)
	{
		setTitle(Messages.getString("OpenDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// images
		Icon thisIcon = new ImageIcon(OpenDialog.class.getResource("images/open.png")); //$NON-NLS-1$
		JLabel thisHeaderLabel = new JLabel();
		thisHeaderLabel.setIcon(thisIcon);
		thisHeaderLabel.setVerticalTextPosition(JLabel.TOP);
		thisHeaderLabel.setHorizontalTextPosition(JLabel.CENTER);
		thisHeaderLabel.setFont(new Font("Dialog", Font.BOLD, 16)); //$NON-NLS-1$
		thisHeaderLabel.setText(Messages.getString("OpenDialog.1")); //$NON-NLS-1$
		thisHeaderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		// labels
		JLabel thisSourceLabel = new JLabel(Messages.getString("OpenDialog.5")); //$NON-NLS-1$
		JLabel thisProviderLabel = new JLabel(Messages.getString("OpenDialog.12")); //$NON-NLS-1$

		// text
		ListCellRenderer thisRenderer = new DefaultListCellRenderer()
		{
			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
			 */
			@Override
			public Component getListCellRendererComponent(JList thisList, Object thisValue, int thisIndex, boolean thisIsSelected, boolean thisCellHasFocus)
			{
				String thisString = (String) thisValue;
				if (thisString != null)
				{
					int thisPosition = thisString.lastIndexOf('/');
					if (thisPosition != -1)
						thisString = thisString.substring(thisPosition + 1);
				}
				return super.getListCellRendererComponent(thisList, thisString, thisIndex, thisIsSelected, thisCellHasFocus);
			}
		};
		theSourceTextField = new JTextField(32);
		theProviderComboBox = new JComboBox();
		theProviderComboBox.setEditable(true);
		theProviderComboBox.setRenderer(thisRenderer);
		theProviderComboBox.setPreferredSize(new Dimension(300, 20));
		if (theseProviders != null)
			for (String thisItem : theseProviders)
				theProviderComboBox.addItem(thisItem);

		// tooltips
		theSourceTextField.setToolTipText(Messages.getString("OpenDialog.6")); //$NON-NLS-1$
		theProviderComboBox.setToolTipText(Messages.getString("OpenDialog.15")); //$NON-NLS-1$

		// buttons
		JButton thisSourceBrowseButton = new JButton(Messages.getString("OpenDialog.9")); //$NON-NLS-1$
		JButton thisProviderAddButton = new JButton(Messages.getString("OpenDialog.10")); //$NON-NLS-1$
		JButton thisOKButton = new JButton(Messages.getString("OpenDialog.2")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("OpenDialog.3")); //$NON-NLS-1$

		// panels
		theDataPanel = new JPanel();
		theDataPanel.setLayout(new GridBagLayout());
		theDataPanel.add(thisSourceLabel, new GridBagConstraints(0, 0, 1, 1, 0., 0., GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));
		theDataPanel.add(thisProviderLabel, new GridBagConstraints(0, 2, 1, 1, 0., 0., GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));
		theDataPanel.add(theSourceTextField, new GridBagConstraints(0, 1, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(theProviderComboBox, new GridBagConstraints(0, 3, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 10), 0, 0));
		theDataPanel.add(thisSourceBrowseButton, new GridBagConstraints(1, 1, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));
		theDataPanel.add(thisProviderAddButton, new GridBagConstraints(1, 3, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 0, 20), 0, 0));

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisOKButton);

		// action
		thisSourceBrowseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisFolder = FileDialogs.getXmlUrl(theProperties.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisFolder != null && !thisFolder.isEmpty())
					theSourceTextField.setText(thisFolder);
			}
		});
		thisProviderAddButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				String thisProvider = ask(Messages.getString("OpenDialog.15")); //$NON-NLS-1$
				if (thisProvider != null && !thisProvider.isEmpty())
				{
					theProviderComboBox.addItem(thisProvider);
					theProviderComboBox.getEditor().setItem(thisProvider);
				}
			}
		});
		thisOKButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				ok = true;
				setVisible(false);
			}
		});
		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent event)
			{
				setVisible(false);
			}
		});
		
		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		thisPanel.add(thisHeaderLabel);
		thisPanel.add(theDataPanel);
		thisPanel.add(thisButtonPanel);
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));

		setContentPane(thisPanel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			ok = false;

			// read properties into components
			theSourceTextField.setText(theProperties.getProperty("source")); //$NON-NLS-1$
			String thisValue = theProperties.getProperty("provider"); //$NON-NLS-1$
			theProviderComboBox.addItem(thisValue);
			theProviderComboBox.getEditor().setItem(thisValue);

			pack();
			Utils.center(this);
		}
		else
		{
			if (ok)
			{
				// update properties from components
				theProperties.setProperty("source", theSourceTextField.getText()); //$NON-NLS-1$
				theProperties.setProperty("provider", (String) theProviderComboBox.getEditor().getItem()); //$NON-NLS-1$
			}
		}
		super.setVisible(thisFlag);
	}

	/**
	 * Ask
	 * 
	 * @param thisMessage
	 *        message
	 * @return input
	 */
	protected String ask(String thisMessage)
	{
		String[] theseLines = thisMessage.split("\n"); //$NON-NLS-1$
		return JOptionPane.showInputDialog(null, theseLines);
	}

	/**
	 * Inform dialog
	 * 
	 * @param thisMessage
	 *        message
	 */
	protected void inform(String thisMessage)
	{
		String[] theseLines = thisMessage.split("\n"); //$NON-NLS-1$
		JOptionPane.showMessageDialog(null, theseLines, Messages.getString("OpenDialog.22"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
	}
}
